package com.danceforyou.customer.controller;

import com.danceforyou.common.model.view.BizData4Page;
import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.common.util.StringUtil;
import com.danceforyou.customer.model.CustomerDO;
import com.danceforyou.customer.service.CustomerService;
import com.danceforyou.customer.service.CustomerUserService;
import com.danceforyou.customer.vo.CustomerVO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerUserService customerUserService;
    @Autowired(required = false)
    private HttpSession session;

    /**
     * 处理客户查询条件
     * @param condition 查询条件
     */
    public void dealCustomerCondition(Map<String, Object> condition) {
        for (Map.Entry<String, Object> entry : condition.entrySet()) {
            if (StringUtil.isBlank(entry.getValue().toString())) {
                condition.put(entry.getKey(), null);
            }
        }
        if (condition.get("createUserId") != null && condition.get("createUserId").toString().equals("ALL")) {
            condition.put("createUserId", null);
        }
        if (condition.get("billPoint") != null && condition.get("billPoint").toString().equals("ALL")) {
            condition.put("billPoint", null);
        }
        if (condition.get("status") != null && condition.get("status").toString().equals("ALL")) {
            condition.put("status", null);
        }
    }

    /**
     * 分页查询客户列表
     * @param bizData4Page BizData4Page
     * @return BizData4Page
     */
    @RequestMapping(value = "/queryCustomerPage", method = RequestMethod.GET)
    public BizData4Page<CustomerVO> queryCustomerPage(BizData4Page<CustomerVO> bizData4Page) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        Map<String, Object> condition = bizData4Page.getCondition();
        dealCustomerCondition(condition);
        condition.put("isDelete", false);
        customerService.queryPageByDataPerm(bizData4Page);
        return bizData4Page;
    }

    /**
     * 分页查询我的客户列表
     * @param bizData4Page BizData4Page
     * @return BizData4Page
     */
    @RequestMapping(value = "/querySelfCustomerPage", method = RequestMethod.GET)
    public BizData4Page<CustomerVO> querySelfCustomerPage(BizData4Page<CustomerVO> bizData4Page) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        Map<String, Object> condition = bizData4Page.getCondition();
        dealCustomerCondition(condition);
        condition.put("isDelete", false);
        condition.put("belongUserId", loginUser.getId());
        customerService.queryPageByDataPerm(bizData4Page);
        return bizData4Page;
    }

    /**
     * 新建客户
     * @param customer 客户信息
     * @return boolean
     */
    @RequestMapping(value = "/saveCustomerInfo", method = RequestMethod.POST)
    public boolean saveCustomerInfo(CustomerDO customer) {
        customerService.saveInfo(customer);
        return true;
    }

    /**
     * 修改客户
     * @param customer 客户信息
     * @return boolean
     */
    @RequestMapping(value = "/updateCustomerInfo", method = RequestMethod.POST)
    public boolean updateCustomerInfo(CustomerDO customer) {
        customerService.updateInfo(customer);
        return true;
    }

    /**
     * 删除客户
     * @param ids 客户IDS
     * @return boolean
     */
    @RequestMapping(value = "/deleteCustomerInfoByIds", method = RequestMethod.POST)
    public int deleteCustomerInfoByIds(String ids) {
        return customerService.deleteInfoByIds(ids);
    }

    /**
     * 启用客户
     * @param customerId 客户ID
     * @return boolean
     */
    @RequestMapping(value = "/setCustomerEnable", method = RequestMethod.POST)
    public boolean setCustomerEnable(Long customerId) {
        customerService.setCustomerEnable(customerId);
        return true;
    }

    /**
     * 停用客户
     * @param customerId 客户ID
     * @return boolean
     */
    @RequestMapping(value = "/setCustomerDisable", method = RequestMethod.POST)
    public boolean setCustomerDisable(Long customerId) {
        customerService.setCustomerDisable(customerId);
        return true;
    }

    /**
     * 保存客户授权员工
     * @param customerId 客户ID
     * @param userIds 员工IDS
     * @return boolean
     */
    @RequestMapping(value = "/saveCustomerUser", method = RequestMethod.POST)
    public boolean saveCustomerUser(Long customerId, String userIds) {
        customerUserService.saveCustomerUser(customerId, userIds);
        return true;
    }

    /**
     * 获取我的客户列表
     * @return List<CustomerVO>
     */
    @RequestMapping(value = "listSelfCustomer", method = RequestMethod.GET)
    public List<CustomerVO> listSelfCustomer() {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        return customerService.queryList(new HashMap<String, Object>(){{
            put("isDelete", false);
            put("status", Constants.CUSTOMER_STATUS_ENABLE);
            put("belongUserId", loginUser.getId());
        }});
    }
}
